﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ShadowCommando.GameMenus
{
    /// <summary>
    /// Reprezentuje przyciski w menu
    /// </summary>
    public enum NameButton {
        /// <summary>
        /// NameText
        /// </summary>
        NameText,
        /// <summary>
        /// Settings
        /// </summary>
        Settings,
        /// <summary>
        /// Text
        /// </summary>
        Text, 
        /// <summary>
        /// NewGame
        /// </summary>
        NewGame, 
        /// <summary>
        /// LoadGame
        /// </summary>
        LoadGame, 
        /// <summary>
        /// Sounds
        /// </summary>
        Sounds, 
        /// <summary>
        /// MainMenu
        /// </summary>
        MainMenu, 
        /// <summary>
        /// Exit
        /// </summary>
        Exit,
        /// <summary>
        /// Controls
        /// </summary>
        Controls,
        /// <summary>
        /// Highscores
        /// </summary>
        Highscores, 
        /// <summary>
        /// SaveGame
        /// </summary>
        SaveGame, 
        /// <summary>
        /// Save
        /// </summary>
        Save, 
        /// <summary>
        /// MusicOnOff
        /// </summary>
        MusicOnOff, 
        /// <summary>
        /// SoundOnOff
        /// </summary>
        SoundOnOff, 
        /// <summary>
        /// Easy
        /// </summary>
        Easy, 
        /// <summary>
        /// Medium
        /// </summary>
        Medium,
        /// <summary>
        /// Hard
        /// </summary>
        Hard, 
        /// <summary>
        /// SaveGameListBox
        /// </summary>
        SaveGameListBox, 
        /// <summary>
        /// LoadGameListBox
        /// </summary>
        LoadGameListBox, 
        /// <summary>
        /// HighscoresListBox
        /// </summary>
        HighscoresListBox ,
        /// <summary>
        /// NameTextBox
        /// </summary>
        NameTextBox};
    /// <summary>
    /// Reprezentuje różne rodzaje menu
    /// </summary>
    public enum NameMenu {
        /// <summary>
        /// NewGameMenu
        /// </summary>
        NewGameMenu, 
        /// <summary>
        /// LoadGameMenu
        /// </summary>
        LoadGameMenu, 
        /// <summary>
        /// SoundsMenu
        /// </summary>
        SoundsMenu, 
        /// <summary>
        /// MainMenu
        /// </summary>
        MainMenu, 
        /// <summary>
        /// ControlsMenu
        /// </summary>
        ControlsMenu, 
        /// <summary>
        /// HighscoresMenu
        /// </summary>
        HighscoresMenu, 
        /// <summary>
        /// SaveGameMenu
        /// </summary>
        SaveGameMenu,
        /// <summary>
        /// SettingsMenu
        /// </summary>
        SettingsMenu,};
    /// <summary>
    /// Kontrolka zawierająca Menu gry dziedziczy po Panel
    /// </summary>
    public partial class GameMenu : Panel
    {
        private string playerName = "Player";
        private int buttonGap = GameMenuSettings.Default.buttonGap;
        private  Dictionary<NameMenu, Panel> controlPanelDictionary;
        private  Dictionary<NameButton, Control> controlDictionary;
        /// <summary>
        /// Konstruktor 
        /// </summary>
    
        public GameMenu()
        {
            InitializeComponent();
            this.DoubleBuffered = true;
            this.AutoSize = true;
            this.BackColor = Color.Transparent;

            Init();
            CreateMenus();
            
        }
        /// <summary>
        /// Słownik zawierający Menu
        /// </summary>
        public Dictionary<NameMenu,Panel> ControlPanelDictionary
        {
            get { return controlPanelDictionary; }  
        }
        /// <summary>
        /// Słownik zawierający kontrolki
        /// </summary>
        public Dictionary<NameButton,Control> ControlDictionary
        {
            get { return controlDictionary; }
        }
        private void CreateMenus()
        {
            CreateMainMenu();
            CreateSoundsMenu();
            CreateHighScoresMenu();
            CreateNewGameMenu();
            CreateControlsMenu();
            CreateSettingsMenu();
        }
        /// <summary>
        /// Start Menu
        /// </summary>
        public void Start()
        {
            ShowMenu(controlPanelDictionary[NameMenu.MainMenu]);
        }
        private void Init()
        {
            controlDictionary = new Dictionary<NameButton, Control>();
            controlPanelDictionary = new Dictionary<NameMenu, Panel>();
        }
        private void AddControlToDictionary(Control control)
        {

            if (controlDictionary.ContainsKey((NameButton) control.Tag))
            {
                control = controlDictionary[((NameButton) control.Tag)];
            }
            else
            {
                controlDictionary.Add(((NameButton) control.Tag), control);
            }


        }
        /// <summary>
        /// PlayerName
        /// </summary>
        public string PlayerName
        {
            get
            {
                return playerName;
            }
            set
            {
                playerName = value;
            }
        }
        private Panel CreatePanel(NameMenu name)
        {
            Panel panel = new Panel();
            panel.Name = name.ToString();
            panel.AutoSize = true;
            panel.Tag = name;
            return panel;
        }
        private int MaxWidth(Panel panel)
        {
            int max = 0;
            foreach (Control c in panel.Controls)
            {
                if (max < c.Width)
                    max = c.Width;
            }
            return max;
        }
        private void SetPanelControls(Panel panel)
        {
            int top = 0;
            int w = MaxWidth(panel);
            for (int i = 0; i < panel.Controls.Count; i++)
            {

                Control control = panel.Controls[i];
                control.Top = top;
                control.Left = w / 2 - control.Width / 2;
                top = control.Bottom + buttonGap;
                if (this.Width < control.Width)
                    this.Width = control.Width;
                AddControlToDictionary(control);
            }

        }
        private void LoadButtonMenuEvents(Panel panel)
        {
            for (int i = 0; i < panel.Controls.Count; i++)
            {
                ButtonMenu bm = panel.Controls[i] as ButtonMenu;
                if (bm != null)
                {
                    bm.Label.Click += new EventHandler(OnGameButtonClick);
                }
            }
        }
        private void CreateSettingsMenu()
        {
            Panel panel = CreatePanel(NameMenu.SettingsMenu);
            panel.Controls.Add(new ButtonMenu(NameButton.MainMenu, "Main menu"));
            panel.Controls.Add(new ButtonMenu(NameButton.NameText, "Player name:"));

            TextBox textBox = new TextBox();
            textBox.Text = playerName;
            textBox.Width = 200;
            textBox.Height = 50;
            textBox.Tag = NameButton.NameTextBox;
            textBox.TextChanged += Test;
            panel.Controls.Add(textBox);

            
            SetPanelControls(panel);
            LoadButtonMenuEvents(panel);
            controlPanelDictionary.Add((NameMenu)panel.Tag, panel);
            panel.Visible = false;

            this.Controls.Add(panel);
        }
        private void CreateMainMenu()
        {
            Panel panel = CreatePanel(NameMenu.MainMenu);
            panel.Controls.Add(new ButtonMenu(NameButton.NewGame, "New game"));
            panel.Controls.Add(new ButtonMenu(NameButton.Sounds, "Sounds"));
            panel.Controls.Add(new ButtonMenu(NameButton.Settings, "Settings"));
            panel.Controls.Add(new ButtonMenu(NameButton.Controls, "Controls"));
            panel.Controls.Add(new ButtonMenu(NameButton.Highscores, "Highscores"));
            panel.Controls.Add(new ButtonMenu(NameButton.Exit, "Exit"));
            SetPanelControls(panel);
            LoadButtonMenuEvents(panel);
            controlPanelDictionary.Add((NameMenu)panel.Tag, panel);
            panel.Visible = false;

            this.Controls.Add(panel);
        }
        private void CreateControlsMenu()
        {
            Panel panel = CreatePanel(NameMenu.ControlsMenu);
            panel.Controls.Add(new ButtonMenu(NameButton.MainMenu, "Main menu"));
            panel.Controls.Add(new ButtonMenu(NameButton.Text, "Up W"));
            panel.Controls.Add(new ButtonMenu(NameButton.Text, "Down S"));
            panel.Controls.Add(new ButtonMenu(NameButton.Text, "Left A"));
            panel.Controls.Add(new ButtonMenu(NameButton.Text, "Right D"));
            panel.Controls.Add(new ButtonMenu(NameButton.Text, "Change Weapon Q"));
            panel.Controls.Add(new ButtonMenu(NameButton.Text, "Bomb E"));
            panel.Controls.Add(new ButtonMenu(NameButton.Text, "Pause P"));
            panel.Controls.Add(new ButtonMenu(NameButton.Text, "Mission Aim M"));

            panel.Visible = false;
            SetPanelControls(panel);
            LoadButtonMenuEvents(panel);
            controlPanelDictionary.Add((NameMenu)panel.Tag, panel);
            
            this.Controls.Add(panel);
           
        }
        private void CreateSoundsMenu()
        {
            Panel panel = CreatePanel(NameMenu.SoundsMenu);
            panel.Controls.Add(new ButtonMenu(NameButton.MainMenu, "Main menu"));
            panel.Controls.Add(new ButtonMenu(NameButton.SoundOnOff, "Sound off"));
          
            
            panel.Visible = false;
            SetPanelControls(panel);
            LoadButtonMenuEvents(panel);
            controlPanelDictionary.Add((NameMenu)panel.Tag, panel);
            this.Controls.Add(panel);
        }
        private void CreateHighScoresMenu()
        {
            Panel panel = CreatePanel(NameMenu.HighscoresMenu);
            panel.Controls.Add(new ButtonMenu(NameButton.MainMenu, "Main menu"));
            panel.Controls.Add(CreateListBox(NameButton.HighscoresListBox, false));
            panel.Font = ListBoxSettings.Default.font;
            panel.Visible = false;
            SetPanelControls(panel);
            LoadButtonMenuEvents(panel);
            controlPanelDictionary.Add((NameMenu)panel.Tag, panel);
            this.Controls.Add(panel);
        }
        private void CreateNewGameMenu()
        {

            Panel panel = CreatePanel(NameMenu.NewGameMenu);
            panel.Controls.Add(new ButtonMenu(NameButton.MainMenu, "Main menu"));
            panel.Controls.Add(new ButtonMenu(NameButton.Easy, "Padawan level"));
            panel.Controls.Add(new ButtonMenu(NameButton.Medium, "Jedi level"));
            panel.Controls.Add(new ButtonMenu(NameButton.Hard, "Master Jedi level"));

            panel.Visible = false;
            SetPanelControls(panel);
            LoadButtonMenuEvents(panel);
            controlPanelDictionary.Add((NameMenu)panel.Tag, panel);
            this.Controls.Add(panel);
        }
        private ListBox CreateListBox(NameButton name, bool enabled)
        {
            ListBox listBox = new ListBox();

            listBox.Name = name.ToString();
            listBox.Enabled = enabled;
            listBox.Width = ListBoxSettings.Default.Width;
            listBox.Height = ListBoxSettings.Default.Height;
            listBox.ForeColor = ListBoxSettings.Default.ForeColor;
            listBox.BackColor = ListBoxSettings.Default.BackColor; ;
            listBox.Tag = name;
            return listBox;
        }
        private void HideMenus()
        {
            foreach (Panel panel in controlPanelDictionary.Values)
                panel.Visible = false;
        }
        private void ShowMenu(Panel panel)
        {
            HideMenus();

            panel.Visible = true;
        }
        private void Test(object sender, EventArgs e)
        {
            TextBox txtBox=sender as TextBox;
            playerName = txtBox.Text;
        }
        private void OnGameButtonClick(object sender, EventArgs e)
        {
            Label label = sender as Label;
            NameButton name = (NameButton)label.Tag;

            switch (name)
            {
                
                case NameButton.MainMenu:
                    {
                        ShowMenu(controlPanelDictionary[NameMenu.MainMenu]);
                        break;
                    }
                case NameButton.Highscores:
                    {
                        ShowMenu(controlPanelDictionary[NameMenu.HighscoresMenu]);
                        break;
                    }
                case NameButton.Sounds:
                    {
                        ShowMenu(controlPanelDictionary[NameMenu.SoundsMenu]);
                        break;
                    }
                case NameButton.Exit:
                    {
                        this.Dispose();
                        break;
                    }
                case NameButton.NewGame:
                    {
                        ShowMenu(controlPanelDictionary[NameMenu.NewGameMenu]);
                        break;
                    }
                case NameButton.Controls:
                    {
                        ShowMenu(controlPanelDictionary[NameMenu.ControlsMenu]);
                        break;
                    }
                case NameButton.Settings:
                    {
                        ShowMenu(controlPanelDictionary[NameMenu.SettingsMenu]);
                        break;
                    }
            }
        }
    }
}
